package com.kaishengit.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.kaishengit.dao.core.BaseDao;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.User;

@Repository
public class TaskDao extends BaseDao<Task,Integer>{

	@SuppressWarnings("unchecked")
	public List<Task> findAllByUserAndType(User user, int typeid, String type) {
		Criteria cri = getSession().createCriteria(Task.class);
		cri.add(Restrictions.eq("type", type));
		cri.add(Restrictions.eq("typeid", typeid));
		cri.add(Restrictions.eq("user", user));
		cri.addOrder(Order.asc("endtime"));
		return cri.list();
	}

	@SuppressWarnings("unchecked")
	public List<Task> findAllByUser(User user) {
		Criteria cri = getSession().createCriteria(Task.class);
		cri.add(Restrictions.eq("user", user));
		return cri.list();
	}

	public Task findMax() {
		Criteria cri = getSession().createCriteria(Task.class);
		cri.setProjection(Projections.max("id"));
		Object id = cri.uniqueResult();
		return get((Integer) id);
	}

}
